package smetana.util;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.List;
import org.apache.pivot.util.CalendarDate;
import org.apache.pivot.wtk.ApplicationContext;

/**
 * @author dmitry.mamonov
 */
public class Pivot {
    private static final SimpleDateFormat nowFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    private static final SimpleDateFormat dateRuFormat = new SimpleDateFormat("dd/MM/yyyy");

    public static <T, S> List<T> iterableToList(Iterable<S> source, Function<S, T> transform) {
        List<T> result = new ArrayList<T>();
        for (T target : Iterables.transform(source, transform)) {
            result.add(target);
        }
        return result;
    }

    public static <T> List<T> asList(T... values) {
        List<T> result = new ArrayList<T>();
        for (T value : values) {
            result.add(value);
        }
        return result;
    }

    public static void println(Object obj) {
        ToStringBuilder.setDefaultStyle(ToStringStyle.MULTI_LINE_STYLE);
        ToStringBuilder tsb = new ReflectionToStringBuilder(obj);
        System.out.println(tsb.toString());
    }

    public static CalendarDate toCalendarDate(java.util.Date date) {
        if (date == null) {
            return null;
        } else {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(date);
            return new CalendarDate(gc);
        }
    }

    public static Date toDate(CalendarDate calendarDate) {
        if (calendarDate == null) {
            return null;
        } else {
            return new Date(calendarDate.toCalendar().getTimeInMillis());
        }
    }

    public static <T> List<T> fromList(java.util.List<T> fromList) {
        if (fromList == null) {
            return new ArrayList<T>();
        } else {
            List<T> result = new ArrayList<T>();
            for (T t : fromList) {
                result.add(t);
            }
            return result;
        }
    }

    public static String now() {
        return nowFormat.format(new Date(System.currentTimeMillis()));
    }

    public static String formatDateRu(Date date) {
        return dateRuFormat.format(date);
    }

    public static void runWork(final Work work) {
        new Thread() {
            @Override
            public void run() {
                try {
                    work.doWork();
                    ApplicationContext.queueCallback(new Runnable() {
                        @Override
                        public void run() {
                            work.publishResults();
                        }
                    });
                } catch (Throwable th) {
                    work.failed(th);
                }
            }
        }.start();
    }
}
